package com.bustle.ejb.service;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.bustle.ejb.entity.Account;
import com.bustle.ejb.entity.User;
import com.bustle.ejb.service.dao.AccountDao;

@Local
public interface AccountManager {
	
	public void createAccount(Account account);
	public Account getByUser(int idUser);

	@Stateless
	public class DefaultAccountManager implements AccountManager {
		
		@PersistenceContext
		private EntityManager entityManager;
		@EJB
		private AccountDao accountDao;

		@Override
		public void createAccount(Account account) {
			accountDao.create(account);
		}

		@Override
		public Account getByUser(int idUser) {
			Account account = null;
			String stringQuery = "SELECT a FROM Account a WHERE user.id = :paramId";
			Query query = entityManager.createQuery(stringQuery);
			query.setParameter("paramId", idUser);
			account = (Account) query.getResultList().get(0);
			return account;
		}
			
	}

}
